package com.xunyin.modules.mainmembersuser.rest;

import cn.hutool.core.bean.BeanUtil;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.aop.ForbidSubmit;
import com.xunyin.modules.membersuser.domain.TMembersUser;
import com.xunyin.modules.membersuser.service.TMembersUserService;
import com.xunyin.modules.membersuser.service.dto.TMembersUserDto;
import com.xunyin.modules.membersuser.service.dto.TMembersUserQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-10-11
 */
@AllArgsConstructor
@Api(tags = "共享空间：会员用户管理")
@RestController
@RequestMapping("api")
public class TMainMembersUserController {
    private final TMembersUserService tMembersUserService;

    @Log("查询共享空间：会员用户")
    @ApiOperation("查询共享空间：会员用户")
    @GetMapping(value = "/tMainMembersUser")
    @PreAuthorize("hasAnyRole('admin','MAINPART_ALL','MAINPART_SELECT')")
    public ResponseEntity getMainMembersUser(TMembersUserQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> queryAll = tMembersUserService.queryAll(criteria, pageable);
        return new ResponseEntity<>(queryAll, HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("新增共享空间：会员用户")
    @ApiOperation("新增共享空间：会员用户")
    @PostMapping(value = "/tMainMembersUser")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity<Object> create(@Validated @RequestBody TMembersUserDto tMembersUserDto) {
        TMembersUser resources = new TMembersUser();
        BeanUtil.copyProperties(tMembersUserDto, resources);
        if (tMembersUserDto.getMembersTimeLengthFlag().equals("1")) {
            resources.setMembersTimeLength(tMembersUserDto.getMembersTimeLength().add(tMembersUserDto.getMembersTimeLengthAdd()));
        } else {
            resources.setMembersTimeLength(tMembersUserDto.getMembersTimeLength().subtract(tMembersUserDto.getMembersTimeLengthAdd()));
        }
        return new ResponseEntity<>(tMembersUserService.save(resources), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改共享空间：会员用户")
    @ApiOperation("修改共享空间：会员用户")
    @PutMapping(value = "/tMainMembersUser")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody TMembersUserDto tMembersUserDto) {
        TMembersUser resources = new TMembersUser();
        BeanUtil.copyProperties(tMembersUserDto, resources);
        if (tMembersUserDto.getMembersTimeLengthFlag().equals("1")) {
            resources.setMembersTimeLength(tMembersUserDto.getMembersTimeLength().add(tMembersUserDto.getMembersTimeLengthAdd()));
        } else {
            resources.setMembersTimeLength(tMembersUserDto.getMembersTimeLength().subtract(tMembersUserDto.getMembersTimeLengthAdd()));
        }
        tMembersUserService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除共享空间：会员设置")
    @ApiOperation("删除共享空间：会员设置")
    @DeleteMapping(value = "/tMainMembersUser/{id}")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        tMembersUserService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
